package com.lankystar.sos.action;

import java.util.HashMap;
import java.util.Map;


import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.lankystar.sos.service.IResponseInterface;
import com.lankystar.util.ResUtil;
import com.opensymphony.xwork2.ActionSupport;

@ParentPackage("sos")
@Namespace("/")
@Component
@Scope("prototype")
@Results({
	@Result(name="success",type="json")
})
public class ResponseAction extends ActionSupport{

	private static final long serialVersionUID = 1L;

	@Autowired
	private IResponseInterface respInterface;
	private Integer userId;
	private Integer sosId;
	private Integer responseId;
	private Integer respkey;
	
	public Integer getUserId() {
		return userId;
	}
	public void setUserId(Integer userId) {
		this.userId = userId;
	}
	public Integer getSosId() {
		return sosId;
	}
	public void setSosId(Integer sosId) {
		this.sosId = sosId;
	}
	public Integer getRespkey() {
		return respkey;
	}
	public void setRespkey(Integer respkey) {
		this.respkey = respkey;
	}
	public Integer getResponseId() {
		return responseId;
	}
	public void setResponseId(Integer responseId) {
		this.responseId = responseId;
	}

	/**
	 * 点击前往施救(添加响应单)
	 * @return
	 */
	@Action(value="addResponseOrder")
	public String addResponseOrderMethod(){
		Map<String, Object> map = new HashMap<>();
		try {
			
			map.put("data",respInterface.addResponseMethod(userId, sosId, respkey));
			map.put("code", 1);
			map.put("msg","施救者响应单生成！！");
		} catch (Exception e) {
			map.put("code", 0);
			map.put("msg","施救者响应单生成失败！！");
		}
		ResUtil.toJson(ServletActionContext.getResponse(), map);
		return SUCCESS;
	}
	/**
	 * 到达指定地点
	 * @return
	 */
	@Action(value="modifyOrder")
	public String modifyOrderMethod(){
		Map<String, Object> map = new HashMap<>();
		try {
			Map<String, Object> maps =  respInterface.updateResponseMethod(responseId, respkey);
			map.put("data", maps);
			if(maps.get("err")==null){
				map.put("code", 1);
				map.put("msg","施救者到达地点，响应单更新成功！！");
			}else{
				map.put("code", 0);
				map.put("msg","响应单更新失败！！！");
			}
		} catch (Exception e) {
			map.put("code", 0);
			map.put("msg","响应单更新失败！");
		}
		ResUtil.toJson(ServletActionContext.getResponse(), map);
		return SUCCESS;
	}
	
}
